using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectBordspil.Models;
using System.Data.Entity;
using System.Web.Security;


namespace ProjectBordspil.Sevices
{
    public class GuiServices
    {
        private IAppRepository _repository = null;

        public GuiServices()
        {
            _repository = new AppRepository();
        }

        public GuiServices(IAppRepository rep)
        {
            _repository = rep;
        }

        /// <summary>
        /// Sets up nobel card deck
        /// </summary>
        public IEnumerable<GuiNobImgInfo> SetUpNobels(int id)
        {
            List<GuiNobImgInfo> gameReady = new List<GuiNobImgInfo>();

            List<GuiImageNobel> aListOfNobels = new List<GuiImageNobel>();

            aListOfNobels = _repository.GetNobels();

            aListOfNobels = aListOfNobels.OrderBy(emp => Guid.NewGuid()).ToList();

            for (var i = 0; i < 6; i++)
            {
                GuiNobImgInfo iImage = new GuiNobImgInfo();

                iImage.idImage = i + 1;
                iImage.idGameInstance = id;
                iImage.nobelObject = aListOfNobels[i];
                iImage.userGotNob = "down";

                gameReady.Add(iImage);
            }

            _repository.AddAllNobImageInto(gameReady);

            return gameReady;
        }

        /// <summary>
        /// Sets up action card deck
        /// </summary>
        public IEnumerable<GuiActImgInfo> SetUpActions(int id)
        {
            List<GuiActImgInfo> gameReady = new List<GuiActImgInfo>();

            List<GuiImageAction> aListOfActions = new List<GuiImageAction>();

            aListOfActions = _repository.GetActions();

            for (var i = 0; i < 5; i++)
            {
                GuiActImgInfo iImage = new GuiActImgInfo();

                iImage.idImage = i + 10;
                iImage.idGameInstance = id;
                iImage.actionObject = aListOfActions[i];

                gameReady.Add(iImage);
            }

            gameReady = gameReady.OrderBy(emp => Guid.NewGuid()).ToList();

            _repository.AddAllActImageInto(gameReady);

            return gameReady;
        }

        /// <summary>
        /// Creates a new Guillontine game
        /// </summary>
        public ActiveGames CreateNewGuillotineGame(string name)
        {

            ActiveGames aNewGame = new ActiveGames();

            Random rnd = new Random();

            int numberOfGame = rnd.Next(1000);

            aNewGame.name = name;
            aNewGame.NumberOfPlayers = 1;
            aNewGame.finished = false;
            aNewGame.nameOfView = "GuilGameView";
            aNewGame.typeOfGame = "Guillontine";
            aNewGame.user1 = Membership.GetUser().UserName;
            aNewGame.user2 = null;

            _repository.AddNewGame(aNewGame);

            return aNewGame;
        }

        /// <summary>
        /// Find an instance of a Guillotine game with some id
        /// </summary>
        public GuiViewModelModel showGameInstace(int id)
        {

            AppDataContext db = new AppDataContext();

            GuiViewModelModel gameIns = new GuiViewModelModel();

            gameIns.theGame = (from game in db.ActiveGames
                               where game.id == id
                               select game).SingleOrDefault();

            gameIns.Actions = (from image in db.GuiActImgInfo.Include(p => p.actionObject)
                               where image.idGameInstance == id
                               select image).ToList();

            gameIns.Nobels = (from image in db.GuiNobImgInfo.Include(p => p.nobelObject)
                              where image.idGameInstance == id
                              select image).ToList();

            return gameIns;

        }

        /// <summary>
        /// Creates a new user
        /// </summary>
        public void CreateNewUser(string userName)
        {

            UserModel newUser = new UserModel();

            newUser.userName = userName;
            newUser.scoreGui = 0;
            newUser.scoreMem = 0;

            _repository.AddUserInfo(newUser);
        }


    }
}
